package com.mt.aop;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * @author Wang
 * @Description TODO 待完善 ,统一日志处理
 * @date 2020/11/11-15:16
 */

@Aspect
@Component
@Slf4j
public class LogAdvice {

    @Pointcut("execution(public * com.mt.controller.*Controller.*(..))")
    public  void  pointcut(){}

    @Before("pointcut()")
    public void beforeLog(JoinPoint point) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //当前用户
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        String[] ParameterNames = methodSignature.getParameterNames();
        Object[] args = point.getArgs();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("参数列表为:");
        for (int i=0;i< ParameterNames.length; i++){
            stringBuilder.append(ParameterNames[i]);
            stringBuilder.append(": ");
            stringBuilder.append(args[i].toString());
        }
        log.info("【请求 URL】：{}", request.getRequestURL());
        log.info("【请求 IP】：{}", request.getRemoteAddr());
        log.info("【请求类名】：{}，【请求方法名】：{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());
        log.info(stringBuilder.toString());
    }


}
